/*
 * @Copyright: 2017 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.third.facade.lianlian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yyfax.commons.http.HttpUtil;
import com.yyfax.pay.third.model.to.lianlian.LianlianExt;
import com.yyfax.pay.third.model.to.lianlian.PayoutOrderLianlianContent;
import com.yyfax.pay.third.model.to.lianlian.PayoutOrderLianlianParams;
import com.yyfax.pay.third.model.to.lianlian.PayoutOrderLianlianReq;
import com.yyfax.pay.third.model.to.lianlian.QueryCardBinLianLianContent;
import com.yyfax.pay.third.model.to.lianlian.QueryCardBinLianLianParams;
import com.yyfax.pay.third.model.to.lianlian.QueryCardBinLianLianReq;
import com.yyfax.pay.third.model.to.lianlian.QueryPayOrderLianlianContent;
import com.yyfax.pay.third.model.to.lianlian.QueryPayOrderLianlianParams;
import com.yyfax.pay.third.model.to.lianlian.QueryPayOrderLianlianReq;
import com.yyfax.pay.third.model.to.lianlian.QueryPayOutOrderLianlianContent;
import com.yyfax.pay.third.model.to.lianlian.QueryPayOutOrderLianlianParams;
import com.yyfax.pay.third.model.to.lianlian.QueryPayOutOrderLianlianReq;
import com.yyfax.pay.third.model.to.lianlian.QueryUserBankcardContent;
import com.yyfax.pay.third.model.to.lianlian.QueryUserBankcardParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * 连连对外接口
 *
 * @author jiangmy
 * @date 2017-04-11 18:50:41
 * @since v1.0.0
 */
@Service
public class LianlianApi {

	private Logger log = LoggerFactory.getLogger(LianlianApi.class);

	/**
	 * 查询连连充值订单Api
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public QueryPayOrderLianlianContent queryPayOrder(QueryPayOrderLianlianParams params) throws Exception {

		String url = params.getUrl();
		QueryPayOrderLianlianReq req = params.getQueryPayOrderLianlianReq();

		log.info("-----------查询连连充值订单[{}]，请求地址={}", req.getNo_order(), url);

		String json = JSON.toJSONString(req);
		String result = HttpUtil.post(url, json);// 发送请求

		// 返回结果json转Map
		QueryPayOrderLianlianContent queryPayOrderLianlianContent = JSON.parseObject(result, new TypeReference<QueryPayOrderLianlianContent>() {
		});

		log.info("-----------查询连连充值订单[{}]，返回参数={}", req.getNo_order(), result);

		return queryPayOrderLianlianContent;
	}

	/**
	 * 查询连连代付订单Api
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public QueryPayOutOrderLianlianContent queryPayOutOrder(QueryPayOutOrderLianlianParams params) throws Exception {
		QueryPayOutOrderLianlianReq req = params.getQueryPayOutOrderLianlianReq();
		String url = params.getUrl();

		log.info("-----------查询连连代付订单[{}]，请求地址={}", req.getNo_order(), url);

		String json = JSON.toJSONString(req);
		String result = HttpUtil.post(url, json);// 发送请求

		// 返回结果json转Map
		QueryPayOutOrderLianlianContent queryPayOutOrderLianlianContent = JSON.parseObject(result, new TypeReference<QueryPayOutOrderLianlianContent>() {
		});

		log.info("-----------查询连连代付订单[{}]，返回参数={}", req.getNo_order(), result);

		return queryPayOutOrderLianlianContent;
	}

	/**
	 * 连连银行卡Bin查询Api
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public QueryCardBinLianLianContent queryCardBin(QueryCardBinLianLianParams params) throws Exception {

		String url = params.getUrl();
		QueryCardBinLianLianReq req = params.getQueryCardBinLianLianReq();
		String json = JSON.toJSONString(req);
		String result = HttpUtil.post(url, json);

		// 返回结果json转Map
		QueryCardBinLianLianContent queryCardBinLianLianContent = JSON.parseObject(result, new TypeReference<QueryCardBinLianLianContent>() {
		});

		log.info("连连银行卡Bin查询结果--[{}]", queryCardBinLianLianContent);

		return queryCardBinLianLianContent;
	}

	/**
	 * 查询用户签约APi
	 *
	 * @param req
	 * @param lianlianExt
	 * @return
	 * @throws Exception
	 */
	public QueryUserBankcardContent readNoAgree(QueryUserBankcardParams req, LianlianExt lianlianExt) throws Exception {
		//发送请求
		String url = lianlianExt.getUserBankcardUrl();

		String json = JSON.toJSONString(req);
		// 发送请求
		String result = HttpUtil.post(url, json);

		QueryUserBankcardContent queryUserBankcardContent = JSON.parseObject(result, new TypeReference<QueryUserBankcardContent>() {
		});
		log.info("获取no_agree结果--{}", queryUserBankcardContent);

		return queryUserBankcardContent;
	}

	/**
	 * 连连代付Api
	 *
	 * @param payoutOrderLianlianParams
	 * @return
	 * @throws Exception
	 */
	public PayoutOrderLianlianContent payout(PayoutOrderLianlianParams payoutOrderLianlianParams) throws Exception {

		String postUrl = payoutOrderLianlianParams.getUrl();
		PayoutOrderLianlianReq payoutOrderLianlianReq = payoutOrderLianlianParams.getPayoutOrderLianlianReq();

		String json = JSON.toJSONString(payoutOrderLianlianReq);
		String result = HttpUtil.post(postUrl, json);

		// 返回结果json转Map
		PayoutOrderLianlianContent payoutOrderYYContent = JSON.parseObject(result, new TypeReference<PayoutOrderLianlianContent>() {
		});

		payoutOrderYYContent.setReqContent(json);
		payoutOrderYYContent.setRspContent(result);
		log.info("连连代付结果--{}", payoutOrderYYContent);

		return payoutOrderYYContent;
	}

}
